'=======================================
' 执行所有测试 case
'=======================================
Private Sub test_ALL_Case()

    ' AddNull 测试
    test_Primitive_AddNull
    ' Add 测试
    test_Obj_append
    ' Add 异常测试
    test_Obj_append_error
    ' setVal 测试
    test_Obj_setVal
    ' setVal 异常测试
    test_Obj_setVal_error
    ' 0 长度初始化，测试自动扩容
    test_Obj_auto_expand_INIT_0
    ' 默认长度初始化，测试自动扩容
    test_Obj_auto_expand_INIT_default
    ' 指定长度初始化，测试自动扩容
    test_Obj_auto_expand_INIT_5
    ' item 获取数据
    test_Obj_auto_getVal
    ' clear 测试，只清空数据引用
    test_Obj_clear_byDefault
    ' clear 测试，清空数据后，重新调整内部数组大小
    test_Obj_clear_by_initCount
    ' remove 测试
    test_Obj_remove
    ' removeLast: 有数据删除测试 + 空引用删除测试
    test_ObjArrayRef_removeLast
    ' pollLast: 有数据删除测试 + 空引用删除测试
    test_ObjArrayRef_pollLast
    ' getLast 测试
    test_ObjArrayRef_getLast
    ' mergeArrRef 测试
    test_ObjArrayRef_mergeArrRef

    ' clone 测试
    test_ObjArrayRef_clone
    ' unsafeRealSize 测试
    test_ObjArrayRef_unsafeRealSize

    ' AddFirst 测试
    test_ObjArrayRef_AddFirst

    ' Insert 测试
    test_ObjArrayRef_Insert
    ' Insert 异常测试 -- 空数组插入，索引 != 0
    test_ObjArrayRef_Insert_error_whenEmpty
    ' Insert 异常测试 -- 数组中存在数据，索引越界 = -1
    test_ObjArrayRef_Insert_error_whenFull_minIndex
    ' Insert 异常测试 -- 数组中存在数据，索引越界 = 索引超过了最大值
    test_ObjArrayRef_Insert_error_whenFull_maxIndex

    ' copyToNewArray 测试
    test_ObjArrayRef_copyToNewArray
End Sub

'=======================================
' IObjArrayRef
'=======================================
' AddNull 测试
Private Sub test_Primitive_AddNull()
    Dim xx As Collection
    Dim yy As Collection
    Dim zz As Collection
    Set xx = New Collection
    Set yy = New Collection
    Set zz = New Collection

    Dim arrRef As IObjArrayRef
    Set arrRef = UtilsArrayRef.createRealObjArrayRef
    arrRef.Add xx

    ' 测试不自动扩容
    arrRef.AddNull 5
    Debug.Assert arrRef.Count = 6
    Debug.Assert arrRef.Item(0) Is xx
    Debug.Assert arrRef.Item(1) Is Nothing

    arrRef.setVal 1, yy
    Debug.Assert arrRef.Item(1) Is yy

    ' 测试自动扩容
    arrRef.AddNull 20
    Debug.Assert arrRef.Count = 26
    Debug.Assert arrRef.Item(0) Is xx
    Debug.Assert arrRef.Item(1) Is yy
    Debug.Assert arrRef.Item(2) Is Nothing
    Debug.Assert arrRef.Item(7) Is Nothing
    Debug.Assert arrRef.Item(25) Is Nothing

    arrRef.setVal 25, zz
    Debug.Assert arrRef.Item(25) Is zz
End Sub

' Add 测试
Private Sub test_Obj_append()
    Dim arrRef As IObjArrayRef
    Set arrRef = UtilsArrayRef.createRealObjArrayRef

    Dim c As Collection
    Set c = New Collection
    arrRef.Add c
    Debug.Assert arrRef.Item(0) Is c
End Sub

' Add 异常测试
Private Sub test_Obj_append_error()
    Dim arrRef As IObjArrayRef
    Set arrRef = UtilsArrayRef.createRealObjArrayRef

On Error Goto catch
    arrRef.Add "xx"
catch:
    Debug.Assert Err.Number = 10011
End Sub

' setVal 测试
Private Sub test_Obj_setVal()
    Dim arrRef As IObjArrayRef
    Set arrRef = UtilsArrayRef.createRealObjArrayRef(10)

    Dim c As Collection
    Set c = New Collection
    Dim c2 As Collection
    Set c2 = New Collection

    arrRef.Add c
    arrRef.setVal 0, c2
    Debug.Assert arrRef.Item(0) Is c2
End Sub

' setVal 异常测试
Private Sub test_Obj_setVal_error()
    Dim arrRef As IObjArrayRef
    Set arrRef = UtilsArrayRef.createRealObjArrayRef(10)

    Dim c As Collection
    Set c = New Collection
    arrRef.Add c

On Error Goto catch
    arrRef.setVal 0, "xxx"
catch:
    Debug.Assert Err.Number = 10011
End Sub

' 0 长度初始化，测试自动扩容
Private Sub test_Obj_auto_expand_INIT_0()
    Dim arrRef As IObjArrayRef
    Set arrRef = UtilsArrayRef.createRealObjArrayRef(0)

    Dim i As Integer
    Dim c As Collection
    For i = 0 To 20
        Set c = New Collection
        arrRef.Add c
    Next

    Debug.Assert arrRef.MaxIndex = 20
    Debug.Assert arrRef.Count = 21
End Sub

' 默认长度初始化，测试自动扩容
Private Sub test_Obj_auto_expand_INIT_default()
    Dim arrRef As IObjArrayRef
    Set arrRef = UtilsArrayRef.createRealObjArrayRef

    Dim i As Integer
    Dim c As Collection
    For i = 0 To 20
        Set c = New Collection
        arrRef.Add c
    Next

    Debug.Assert arrRef.MaxIndex = 20
    Debug.Assert arrRef.Count = 21
End Sub

' 指定长度初始化，测试自动扩容
Private Sub test_Obj_auto_expand_INIT_5()
    Dim arrRef As IObjArrayRef
    Set arrRef = UtilsArrayRef.createRealObjArrayRef(5)

    Dim i As Integer
    Dim c As Collection
    For i = 0 To 20
        Set c = New Collection
        arrRef.Add c
    Next

    Debug.Assert arrRef.MaxIndex = 20
    Debug.Assert arrRef.Count = 21
End Sub

' item 获取数据
Private Sub test_Obj_auto_getVal()
    Dim arrRef As IObjArrayRef
    Set arrRef = UtilsArrayRef.createRealObjArrayRef

    Dim c As Collection
    Set c = New Collection
    arrRef.Add c

    Dim cx As Collection
    Set cx = arrRef.Item(0)
    Debug.Assert cx Is c
End Sub

' clear 测试，只清空数据引用
Private Sub test_Obj_clear_byDefault()
    Dim arrRef As IObjArrayRef
    Set arrRef = UtilsArrayRef.createRealObjArrayRef(5)

    Dim i As Integer
    Dim c As Collection
    For i = 0 To 20
        Set c = New Collection
        arrRef.Add c
    Next

    arrRef.clear
    Debug.Assert arrRef.Count = 0
    Debug.Assert arrRef.MaxIndex = -1
End Sub

' clear 测试，清空数据后，重新调整内部数组大小
Private Sub test_Obj_clear_by_initCount()
    Dim arrRef As IObjArrayRef
    Set arrRef = UtilsArrayRef.createRealObjArrayRef(5)

    Dim i As Integer
    Dim c As Collection
    For i = 0 To 20
        Set c = New Collection
        arrRef.Add c
    Next

    arrRef.clear 2
    Debug.Assert arrRef.Count = 0

    For i = 0 To 20
        Set c = New Collection
        arrRef.Add c
    Next

    Debug.Assert arrRef.Count = 21
    Debug.Assert arrRef.MaxIndex = 20
End Sub

' remove 测试
Private Sub test_Obj_remove()
    Dim arrRef As IObjArrayRef
    Set arrRef = UtilsArrayRef.createRealObjArrayRef(5)

    Dim k0 As Collection
    Dim k1 As Collection
    Dim k2 As Collection
    Dim k3 As Collection
    Dim k4 As Collection
    Set k0 = New Collection
    Set k1 = New Collection
    Set k2 = New Collection
    Set k3 = New Collection
    Set k4 = New Collection

    arrRef.Add k0
    arrRef.Add k1
    arrRef.Add k2
    arrRef.Add k3
    arrRef.Add k4

    ' 1. 索引越界测试
    arrRef.remove -1
    Debug.Assert arrRef.Count = 5
    arrRef.remove 5
    Debug.Assert arrRef.Count = 5

    ' 2. 边界删除测试
    arrRef.remove 0
    Debug.Assert arrRef.Count = 4
    Debug.Assert arrRef.Item(0) Is k1
    Debug.Assert arrRef.Item(1) Is k2
    Debug.Assert arrRef.Item(2) Is k3
    Debug.Assert arrRef.Item(3) Is k4

    arrRef.remove 3
    Debug.Assert arrRef.Count = 3
    Debug.Assert arrRef.Item(0) Is k1
    Debug.Assert arrRef.Item(1) Is k2
    Debug.Assert arrRef.Item(2) Is k3

    ' 3. 删除中间元素
    arrRef.remove 1
    Debug.Assert arrRef.Count = 2
    Debug.Assert arrRef.Item(0) Is k1
    Debug.Assert arrRef.Item(1) Is k3
End Sub

' removeVal 测试
Private Sub test_Primitive_removeVal()
    Dim arrRef As IObjArrayRef
    Set arrRef = UtilsArrayRef.createRealObjArrayRef(5)

    Dim kx As Collection
    Dim k0 As Collection
    Dim k1 As Collection
    Dim k2 As Collection
    Dim others As Collection
    Set kx = New Collection
    Set k0 = New Collection
    Set k1 = New Collection
    Set k2 = New Collection
    Set others = New Collection

    arrRef.Add kx
    arrRef.Add kx
    arrRef.Add k0
    arrRef.Add kx
    arrRef.Add kx
    arrRef.Add kx
    arrRef.Add k1
    arrRef.Add kx
    arrRef.Add k2
    arrRef.Add kx

    ' 1. 不存在数据测试
    arrRef.removeVal others
    Debug.Assert arrRef.Count = 10

    ' 2. 只删除第一个相同的值
    arrRef.removeVal kx
    Debug.Assert arrRef.Count = 9
    Debug.Assert arrRef.Item(0) Is kx
    Debug.Assert arrRef.Item(1) Is k0
    Debug.Assert arrRef.Item(2) Is kx
    Debug.Assert arrRef.Item(3) Is kx
    Debug.Assert arrRef.Item(4) Is kx
    Debug.Assert arrRef.Item(5) Is k1
    Debug.Assert arrRef.Item(6) Is kx
    Debug.Assert arrRef.Item(7) Is k2
    Debug.Assert arrRef.Item(8) Is kx

    ' 3. 删除所有相同的数据
    arrRef.removeVal kx, True
    Debug.Assert arrRef.Count = 3
    Debug.Assert arrRef.Item(0) Is k0
    Debug.Assert arrRef.Item(1) Is k1
    Debug.Assert arrRef.Item(2) Is k2
End Sub

' removeLast: 有数据删除测试 + 空引用删除测试
Private Sub test_ObjArrayRef_removeLast()
    Dim arrRef As IObjArrayRef
    Set arrRef = UtilsArrayRef.createRealObjArrayRef(5)

    Dim c1 As Collection
    Set c1 = New Collection
    Dim c2 As Collection
    Set c2 = New Collection
    Dim c3 As Collection
    Set c3 = New Collection
    Dim c4 As Collection
    Set c4 = New Collection
    Dim c5 As Collection
    Set c5 = New Collection
    Dim c6 As Collection
    Set c6 = New Collection

    arrRef.Add c1
    arrRef.Add c2
    arrRef.Add c3
    arrRef.Add c4
    arrRef.Add c5
    arrRef.Add c6

    arrRef.removeLast
    Debug.Assert arrRef.Count = 5
    Debug.Assert arrRef.Item(0) Is c1
    Debug.Assert arrRef.Item(1) Is c2
    Debug.Assert arrRef.Item(2) Is c3
    Debug.Assert arrRef.Item(3) Is c4
    Debug.Assert arrRef.Item(4) Is c5

    arrRef.removeLast
    Debug.Assert arrRef.Count = 4
    Debug.Assert arrRef.Item(0) Is c1
    Debug.Assert arrRef.Item(1) Is c2
    Debug.Assert arrRef.Item(2) Is c3
    Debug.Assert arrRef.Item(3) Is c4

    arrRef.removeLast
    Debug.Assert arrRef.Count = 3
    Debug.Assert arrRef.Item(0) Is c1
    Debug.Assert arrRef.Item(1) Is c2
    Debug.Assert arrRef.Item(2) Is c3

    arrRef.removeLast
    Debug.Assert arrRef.Count = 2
    Debug.Assert arrRef.Item(0) Is c1
    Debug.Assert arrRef.Item(1) Is c2

    arrRef.removeLast
    Debug.Assert arrRef.Count = 1
    Debug.Assert arrRef.Item(0) Is c1

    arrRef.removeLast
    Debug.Assert arrRef.Count = 0

    ' 空删除测试
    arrRef.removeLast
    Debug.Assert arrRef.Count = 0
    arrRef.removeLast
    Debug.Assert arrRef.Count = 0

End Sub

' pollLast: 有数据删除测试 + 空引用删除测试
Private Sub test_ObjArrayRef_pollLast()
    Dim arrRef As IObjArrayRef
    Set arrRef = UtilsArrayRef.createRealObjArrayRef(5)

    Dim c1 As Collection
    Set c1 = New Collection
    Dim c2 As Collection
    Set c2 = New Collection
    Dim c3 As Collection
    Set c3 = New Collection
    Dim c4 As Collection
    Set c4 = New Collection
    Dim c5 As Collection
    Set c5 = New Collection
    Dim c6 As Collection
    Set c6 = New Collection

    arrRef.Add c1
    arrRef.Add c2
    arrRef.Add c3
    arrRef.Add c4
    arrRef.Add c5
    arrRef.Add c6

    Debug.Assert arrRef.pollLast() Is c6
    Debug.Assert arrRef.Count = 5
    Debug.Assert arrRef.Item(0) Is c1
    Debug.Assert arrRef.Item(1) Is c2
    Debug.Assert arrRef.Item(2) Is c3
    Debug.Assert arrRef.Item(3) Is c4
    Debug.Assert arrRef.Item(4) Is c5

    Debug.Assert arrRef.pollLast() Is c5
    Debug.Assert arrRef.Count = 4
    Debug.Assert arrRef.Item(0) Is c1
    Debug.Assert arrRef.Item(1) Is c2
    Debug.Assert arrRef.Item(2) Is c3
    Debug.Assert arrRef.Item(3) Is c4

    Debug.Assert arrRef.pollLast() Is c4
    Debug.Assert arrRef.Count = 3
    Debug.Assert arrRef.Item(0) Is c1
    Debug.Assert arrRef.Item(1) Is c2
    Debug.Assert arrRef.Item(2) Is c3

    Debug.Assert arrRef.pollLast() Is c3
    Debug.Assert arrRef.Count = 2
    Debug.Assert arrRef.Item(0) Is c1
    Debug.Assert arrRef.Item(1) Is c2

    Debug.Assert arrRef.pollLast() Is c2
    Debug.Assert arrRef.Count = 1
    Debug.Assert arrRef.Item(0) Is c1

    Debug.Assert arrRef.pollLast() Is c1
    Debug.Assert arrRef.Count = 0

    ' 空删除测试
    Debug.Assert arrRef.pollLast() Is Nothing
    Debug.Assert arrRef.Count = 0
    Debug.Assert arrRef.pollLast() Is Nothing
    Debug.Assert arrRef.Count = 0
End Sub



' getLast 测试
Private Sub test_ObjArrayRef_getLast()
    Dim arrRef As IObjArrayRef
    Set arrRef = UtilsArrayRef.createRealObjArrayRef(5)

    Debug.Assert arrRef.getLast() Is Nothing

    Dim c1 As Collection
    Set c1 = New Collection
    Dim c2 As Collection
    Set c2 = New Collection

    arrRef.Add c1
    arrRef.Add c2

    Debug.Assert arrRef.getLast() Is c2
End Sub

' mergeArrRef 测试
Private Sub test_ObjArrayRef_mergeArrRef()
    Dim arrRef As IObjArrayRef
    Set arrRef = UtilsArrayRef.createRealObjArrayRef(5)


    Dim c1 As Collection
    Set c1 = New Collection
    Dim c2 As Collection
    Set c2 = New Collection

    Dim others As IObjArrayRef
    Set others = UtilsArrayRef.createRealObjArrayRef(2)
    others.Add c1
    others.Add c2

    arrRef.mergeArrRef others
    Debug.Assert arrRef.Count = 2
    Debug.Assert arrRef.MaxIndex = 1

    arrRef.mergeArrRef others
    Debug.Assert arrRef.Count = 4
    Debug.Assert arrRef.MaxIndex = 3

    arrRef.mergeArrRef others
    Debug.Assert arrRef.Count = 6
    Debug.Assert arrRef.MaxIndex = 5

    Debug.Assert arrRef.Item(0) Is c1
    Debug.Assert arrRef.Item(1) Is c2
    Debug.Assert arrRef.Item(2) Is c1
    Debug.Assert arrRef.Item(3) Is c2
    Debug.Assert arrRef.Item(4) Is c1
    Debug.Assert arrRef.Item(5) Is c2
End Sub

' 子数组引用 测试
Private Sub test_Primitive_subArray()
    Dim arrRef As IObjArrayRef
    Set arrRef = UtilsArrayRef.createRealObjArrayRef(5)

    Dim c0 As Collection
    Set c0 = New Collection
    Dim c1 As Collection
    Set c1 = New Collection
    Dim c2 As Collection
    Set c2 = New Collection
    Dim c3 As Collection
    Set c3 = New Collection
    Dim c4 As Collection
    Set c4 = New Collection
    Dim c5 As Collection
    Set c5 = New Collection

    arrRef.Add c0
    arrRef.Add c1
    arrRef.Add c2
    arrRef.Add c3
    arrRef.Add c4
    arrRef.Add c5

    Dim arr2 As IObjArrayRef

    ' 测试1:
    ' 如果 `startIdx` >= `endIdx`，将会返回一个空引用
    Set arr2 = arrRef.slice(1, 1)
    Debug.Assert arr2.Count = 0

    Set arr2 = arrRef.slice(2, 1)
    Debug.Assert arr2.Count = 0

    ' 测试2:
    ' 如果 `endIdx` 超过了当前数组引用中元素的数量，则直接截取到数组的结束位置
    Set arr2 = arrRef.slice(3, 100)
    Debug.Assert arr2.Count = 3
    Debug.Assert arr2.Item(0) Is c3
    Debug.Assert arr2.Item(1) Is c4
    Debug.Assert arr2.Item(2) Is c5

    ' 测试3:
    Set arr2 = arrRef.slice(step:= -1)
    Debug.Assert arr2.Count = 4
    Debug.Assert arr2.Item(0) Is c3
    Debug.Assert arr2.Item(1) Is c2
    Debug.Assert arr2.Item(2) Is c1
    Debug.Assert arr2.Item(3) Is c0

    ' 测试4:
    ' 如果 `endIdx` < 0, 将会返回一个空引用
    Set arr2 = arrRef.slice(-99, -100)
    Debug.Assert arr2.Count = 0

    ' 测试5:
    ' 从 0 开始截取
    Set arr2 = arrRef.slice(0, 4)
    Debug.Assert arr2.Count = 4
    Debug.Assert arr2.Item(0) Is c0
    Debug.Assert arr2.Item(1) Is c1
    Debug.Assert arr2.Item(2) Is c2
    Debug.Assert arr2.Item(3) Is c3

    ' 测试6:
    ' 完整截取
    Set arr2 = arrRef.slice(0, 6)
    Debug.Assert arr2.Count = 6
    Debug.Assert arr2.Item(0) Is c0
    Debug.Assert arr2.Item(1) Is c1
    Debug.Assert arr2.Item(2) Is c2
    Debug.Assert arr2.Item(3) Is c3
    Debug.Assert arr2.Item(4) Is c4
    Debug.Assert arr2.Item(5) Is c5
End Sub

' clone 测试
Private Sub test_ObjArrayRef_clone()
    Dim arrRef As IObjArrayRef
    Set arrRef = UtilsArrayRef.createRealObjArrayRef(5)

    Dim c0 As Collection
    Set c0 = New Collection
    Dim c1 As Collection
    Set c1 = New Collection
    Dim c2 As Collection
    Set c2 = New Collection
    Dim c3 As Collection
    Set c3 = New Collection
    Dim c4 As Collection
    Set c4 = New Collection
    Dim c5 As Collection
    Set c5 = New Collection

    arrRef.Add c0
    arrRef.Add c1
    arrRef.Add c2
    arrRef.Add c3
    arrRef.Add c4
    arrRef.Add c5

    Dim arr2 As IObjArrayRef
    Set arr2 = arrRef.clone

    Debug.Assert arr2.Count = 6
    arr2.setVal 1, Nothing

    Debug.Assert Not arrRef.Item(1) Is arr2.Item(1)
    Debug.Assert arr2.Item(1) Is Nothing
    Debug.Assert arrRef.Item(1) Is c1
End Sub

' unsafeRealSize 测试
Private Sub test_ObjArrayRef_unsafeRealSize()
    Dim arrRef As IObjArrayRef
    Set arrRef = UtilsArrayRef.createRealObjArrayRef(5)

    Dim c0 As Collection
    Set c0 = New Collection
    arrRef.Add c0
    arrRef.Add c0
    arrRef.Add c0
    arrRef.Add c0
    arrRef.Add c0
    arrRef.Add c0
    arrRef.Add c0

    ' 5 --> 5 + 5/2 = 7
    Debug.Assert arrRef.unsafeRealSize = 7
End Sub

' AddFirst 测试
Private Sub test_ObjArrayRef_AddFirst()
    Dim arrRef As IObjArrayRef
    Set arrRef = UtilsArrayRef.createRealObjArrayRef

    Dim c1 As Collection
    Set c1 = New Collection
    Dim c2 As Collection
    Set c2 = New Collection
    Dim c3 As Collection
    Set c3 = New Collection

    arrRef.Add c1
    arrRef.AddFirst c2
    Debug.Assert arrRef.Item(0) Is c2
    Debug.Assert arrRef.Item(1) Is c1

    arrRef.AddFirst Nothing
    Debug.Assert arrRef.Item(0) Is Nothing
    Debug.Assert arrRef.Item(1) Is c2
    Debug.Assert arrRef.Item(2) Is c1

    arrRef.AddFirst c3
    Debug.Assert arrRef.Item(0) Is c3
    Debug.Assert arrRef.Item(1) Is Nothing
    Debug.Assert arrRef.Item(2) Is c2
    Debug.Assert arrRef.Item(3) Is c1
End Sub

'--------------------------------------------------------
' Insert 测试
'--------------------------------------------------------
' Insert 测试
Private Sub test_ObjArrayRef_Insert()
    Dim arrRef As IObjArrayRef
    Set arrRef = UtilsArrayRef.createRealObjArrayRef

    Dim c1 As Collection
    Set c1 = New Collection
    Dim c2 As Collection
    Set c2 = New Collection
    Dim c3 As Collection
    Set c3 = New Collection
    Dim c4 As Collection
    Set c4 = New Collection

    ' ------------------- 1. 基础测试---------------------
    arrRef.Add c1
    arrRef.Add c2
    arrRef.Add c3
    Debug.Assert arrRef.Item(0) Is c1
    Debug.Assert arrRef.Item(1) Is c2
    Debug.Assert arrRef.Item(2) Is c3

    arrRef.Insert 2, c4
    Debug.Assert arrRef.Item(0) Is c1
    Debug.Assert arrRef.Item(1) Is c2
    Debug.Assert arrRef.Item(2) Is c4
    Debug.Assert arrRef.Item(3) Is c3

    ' ------------------- 2. 边界测试---------------------
    arrRef.clear

    arrRef.Insert 0, c1
    Debug.Assert arrRef.Item(0) Is c1
    Debug.Assert arrRef.Count = 1

    arrRef.Add c2
    arrRef.Add c3
    Debug.Assert arrRef.Item(0) Is c1
    Debug.Assert arrRef.Item(1) Is c2
    Debug.Assert arrRef.Item(2) Is c3
    Debug.Assert arrRef.Count = 3

    arrRef.Insert 3, c4
    Debug.Assert arrRef.Item(0) Is c1
    Debug.Assert arrRef.Item(1) Is c2
    Debug.Assert arrRef.Item(2) Is c3
    Debug.Assert arrRef.Item(3) Is c4
    Debug.Assert arrRef.Count = 4
End Sub

' Insert 异常测试 -- 空数组插入，索引 != 0
Private Sub test_ObjArrayRef_Insert_error_whenEmpty()
On Error Goto catch
    Dim arrRef As IObjArrayRef
    Set arrRef = UtilsArrayRef.createRealObjArrayRef

    arrRef.Insert -1, c1
    Debug.Assert False
    Exit Sub
catch:
    Debug.Assert Err.Description = "IndexOutOfBounds: [index]=-1 out of bounds. count=0]"
End Sub

' Insert 异常测试 -- 数组中存在数据，索引越界 = -1
Private Sub test_ObjArrayRef_Insert_error_whenFull_minIndex()
On Error Goto catch
    Dim arrRef As IObjArrayRef
    Set arrRef = UtilsArrayRef.createRealObjArrayRef

    Dim c1 As Collection
    Set c1 = New Collection
    Dim c2 As Collection
    Set c2 = New Collection
    Dim c3 As Collection
    Set c3 = New Collection
    arrRef.Add c1
    arrRef.Add c2
    arrRef.Add c3
    Debug.Assert arrRef.Item(0) Is c1
    Debug.Assert arrRef.Item(1) Is c2
    Debug.Assert arrRef.Item(2) Is c3

    arrRef.Insert -1, c1
    Debug.Assert False
    Exit Sub
catch:
    Debug.Assert Err.Description = "IndexOutOfBounds: [index]=-1 out of bounds. count=3]"
End Sub

' Insert 异常测试 -- 数组中存在数据，索引越界 = 索引超过了最大值
Private Sub test_ObjArrayRef_Insert_error_whenFull_maxIndex()
On Error Goto catch
    Dim arrRef As IObjArrayRef
    Set arrRef = UtilsArrayRef.createRealObjArrayRef

    Dim c1 As Collection
    Set c1 = New Collection
    Dim c2 As Collection
    Set c2 = New Collection
    Dim c3 As Collection
    Set c3 = New Collection
    arrRef.Add c1
    arrRef.Add c2
    arrRef.Add c3
    Debug.Assert arrRef.Item(0) Is c1
    Debug.Assert arrRef.Item(1) Is c2
    Debug.Assert arrRef.Item(2) Is c3

    arrRef.Insert 4, c1
    Debug.Assert False
    Exit Sub
catch:
    Debug.Assert Err.Description = "IndexOutOfBounds: [index]=4 out of bounds. count=3]"
End Sub


'--------------------------------------------------------
' copyToNewArray 测试
'--------------------------------------------------------
Private Sub test_ObjArrayRef_copyToNewArray()
    Dim tempRef As IObjArrayRef
    Dim baseRef As IObjArrayRef
    Set baseRef = UtilsArrayRef.createRealObjArrayRef

    ' ------------------- 1. 空拷贝---------------------
    Set tempRef = baseRef.copyToNewArray
    Debug.Assert tempRef.Count = 0

    Set tempRef = baseRef.copyToNewArray(1)
    Debug.Assert tempRef.Count = 0

    Set tempRef = baseRef.copyToNewArray(endIdx := 2)
    Debug.Assert tempRef.Count = 0

    ' ------------------- 2. 边界测试---------------------
    Dim c1 As Collection
    Dim c2 As Collection
    Dim c3 As Collection
    Set c1 = New Collection
    Set c2 = New Collection
    Set c3 = New Collection

    baseRef.Add c1
    baseRef.Add c2
    baseRef.Add c3

    ' 1. 全部拷贝
    Set tempRef = baseRef.copyToNewArray
    Debug.Assert tempRef.Count = 3
    Debug.Assert tempRef.Item(0) Is c1
    Debug.Assert tempRef.Item(1) Is c2
    Debug.Assert tempRef.Item(2) Is c3

    ' 2. 结束索引边界
    Set tempRef = baseRef.copyToNewArray(endIdx := 2)
    Debug.Assert tempRef.Count = 2
    Debug.Assert tempRef.Item(0) Is c1
    Debug.Assert tempRef.Item(1) Is c2

    Set tempRef = baseRef.copyToNewArray(endIdx := 3)
    Debug.Assert tempRef.Count = 3
    Debug.Assert tempRef.Item(0) Is c1
    Debug.Assert tempRef.Item(1) Is c2
    Debug.Assert tempRef.Item(2) Is c3

    ' 3. 起始 == 结束
    Set tempRef = baseRef.copyToNewArray(1, 1)
    Debug.Assert tempRef.Count = 0

    ' 4. 起始 > 结束
    Set tempRef = baseRef.copyToNewArray(2, 1)
    Debug.Assert tempRef.Count = 0

    ' 5. 起始 = 结束 - 1
    Set tempRef = baseRef.copyToNewArray(1, 2)
    Debug.Assert tempRef.Count = 1
    Debug.Assert tempRef.Item(0) Is c2
End Sub
